/**
 * Created by hooshim on 2014-12-04.
 */

var G_grid01 = "";
var G_grid02 = "";
var G_page = 1;

$(document).ready(function() {
    fnGetSubMenus();
    fnSetCurMenu(7);
    fnGetLeftMenus('6');
    $("#_curMenu p").html("지역코드");

    fnComboExpress('','findExpress');
    fnComboExpress('','expressCd');
    var bodyWidth = $('body').innerWidth();
    bodyWidth = parseInt(bodyWidth,10) - 170;
    //$('#_bodyRight').css('width', bodyWidth + 'px');
    $('#_bodyRight').css('width', '840px');
    var gSubMenus = fnGetSubMenus();
    var gScreenHeight = parseInt($(window).height(),10) - 120;
    $("#_bodyLeft").css("height",gScreenHeight);
    $("#_bodyRight").css("height",gScreenHeight);
    gScreenHeight = parseInt(gScreenHeight,10) - 100;

    fnSetGrid01(gScreenHeight);
    fnGetList01(1);
});

var fnSetGrid01 = function(h) {

    var grdCd = "grid01";

    var widths = [60, 70, 60, 206, 50];
    var titles = ['Express', '등급', '지역', '국가', '코드'];
    var styles = ['text-align:center', 'text-align:center;', 'text-align:center;', 'text-align:left;', 'text-align:center;'];

    var gInfo = new Object();
    gInfo["grdCd"] = "grid01";
    gInfo["title"] = "지역코드";
    gInfo["widths"] = widths;
    gInfo["titles"] = titles;
    gInfo["styles"] = styles;
    gInfo["function"] = "fnShowZone";
    gInfo["fullWidth"] = "500";
    gInfo["fullHeight"] = h;
    gInfo["fix"] = "1";

    G_grid01 = JSON.stringify(gInfo);

    fnMkGridInit(G_grid01);
}

var fnGetList = function() {

    if(event.keyCode != "13")
    {
        return; // 엔터일때만 수행
    }
    fnGetList01(1);
}

var fnGetList01 = function(page) {
    fnMkGridLoad(G_grid01);
    if (page) G_page = page;
    else page = G_page;
    var param  = "findExpress="+$("#findExpress").val();
    param  += "&findNation="+$("#findNation").val();
    var valUrl = "./action/zone/list.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){

            fnMkGrid(G_grid01,data);
            /*
             $("#table_body").html(list);
             $("#total_rows").html(data.TOTAL_ROW_COUNT);
             var paging = fnPaging('fnGetList', data.TOTAL_ROW_COUNT, page, 10);
             var pages = paging['prev'] + paging['pager'] + paging['next'];
             // alert(pages);
             $("#pager").html(pages);
             */
        },
        error: function(data){
            alert('Error!!');
            alert(data.responseText);
        }
    });
}

var fnShowZone = function(seq) {

    var param  = "seq="+seq;
    var valUrl = "./action/zone/view.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){
            fnReset();

            var dataDetail = data.TBLEXPRESSZONE[0];

            $("#expressCd").val(dataDetail.EXPRESSCD);
            $("#class").val(dataDetail.CLASS);
            $("#nationCd").val(dataDetail.NATIONCD);
            $("#flag").val(dataDetail.FLAG);
            $("#seq").val(dataDetail.SEQ);
            fnComboZone(dataDetail.EXPRESSCD,'zoneCd');
            setTimeout('$("#zoneCd").val("'+dataDetail.ZONECD+'");',1000);

        },
        error: function(request,status,error){
            alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
        }
    });
}

var fnSave = function() {
    if (!$("#expressCd").val()) {
        alert("Express를 바르게 선택해 주세요.");
        $("#expressCd").focus();
        return;
    }
    if (!$("#zoneCd").val()) {
        alert("지역을 바르게 선택해 주세요.");
        $("#zoneCd").focus();
        return;
    }
    if (!$("#nationCd").val()) {
        alert("국가코드를 바르게 입력해 주세요.");
        $("#nationCd").focus();
        return;
    }

    var param = "nationCd=" + $("#nationCd").val();
    param += "&expressCd=" + $("#expressCd").val();
    param += "&zoneCd=" + $("#zoneCd").val();
    param += "&class=" + $("#class").val();
    param += "&flag=" + $("#flag").val();
    param += "&seq=" + $("#seq").val();

    var valUrl = "./action/zone/save.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){
            if (data.rsStatus == "Success") {
                alert(data.rsStr);
                fnReset();
                fnGetList01();
            }
            else {
                alert("저장 실패");
            }
        },
        error: function(request,status,error){
            alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
        }
    });
}

var fnReset = function() {

    $("#seq").val('');
    $("#expressCd").val('');
    $("#zoneCd").val('');
    $("#class").val('N');
    $("#nationCd").val('');
    $("#flag").val('Y');
}
